using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MvcSort.Models;
using MvcSort.Repan;

namespace MvcSort.Controllers
{
    public class UserController:Controller
    {
        public IActionResult index(string id)
        {   
            
            var lirep = new EFrepost<BookLibary>();
            var li = lirep.Table.FirstOrDefault();
            if (li==null)
            {
                lirep.Insert(new BookLibary{
                    LibaryName="无敌"
                });
            }
           if (id==null)
           {
                var bookrep = new EFrepost<Book>();
            var book = bookrep.Table.Where(x=>x.ISDeleted==false);
             return View(book);
           }
           else
           {
                var bookrep = new EFrepost<Book>();
            var book = bookrep.Table.Where(x=>x.ISDeleted==false && x.BookName.Contains(id));
            return View(book);
           }
            
        }

        public IActionResult createindex(int id)
        {   
            var bookrep = new EFrepost<Book>();
            if (id==0)
            {
             return View();   
            }else
            {
                var book = bookrep.Table.FirstOrDefault(x=>x.Id==id);
                bookrep.Update(book);
                return View(book);
            }
            
        }
        public IActionResult save(Repans res)
        {
            var bookrep = new EFrepost<Book>();
            if(res.Id==0)
            {
                bookrep.Insert(new Book{
                    BookName=res.BookName,
                    Id=res.Id,
                    ISBN=res.ISBN,
                    bookLibaryId=1
                });
            }else
            {
                var book = bookrep.Table.FirstOrDefault(x=>x.Id==res.Id);
                book.BookName=res.BookName;
                book.Id=res.Id;
                book.ISBN=res.ISBN;
                bookrep.Update(book);
            }
            return Ok();
        }
        public IActionResult delete(int id)
        {
            var bookrep = new EFrepost<Book>();
            var book = bookrep.Table.Where(x=>x.Id==id);
            bookrep.Delete(id);
            return RedirectToAction("index");
        }

    }
}